<?php
class Role extends MY_Controller {
	const PERM_UPDATE = 0;
	const PERM_INSERT = 1;
	protected $_controller_path = '/role/Role';
	protected $_list_path = '/role/list';
	protected $_model = 'role/Role_model';
	protected $_view_path = 'role/view';
	protected $_create_path = 'role/create';
	protected $_edit_path = 'role/edit';
	protected $_resource_id = self::ROLE_RESOURCE;

	function __construct() {
		parent::__construct();
		$this -> load -> model('permission/Permission_model', 'permission');
		
	}

	protected function setFormValidation() {
		$this -> form_validation -> set_rules('name', 'Name', 'trim|required');
	}

	protected function init_input_data() {
		$role_name = $this -> input -> post('name');
		$description = $this -> input -> post('description');
		$meta_data = unsign($role_name.self::META_SEPERATE.$description);
		return array('name' => $role_name,
					'description'=>$description,
					'meta_data' => $meta_data
		);
	}
	
	function create() {
		$this -> setFormValidation();
		if ($this -> form_validation -> run() == false) {
			$this->load->view($this -> _create_path, array());
		} else {
		$role_id = $this -> model -> insert($this -> init_input_data());

		$user_resource = $this -> input -> post('user_resource');
		$user_perm = $this -> input -> post('user_perm');
		
		$role_resource = $this -> input -> post('role_resource');
		$role_perm = $this -> input -> post('role_perm');
		
		$article_resource = $this -> input -> post('article_resource');
		$article_perm = $this -> input -> post('article_perm');
		
		$catalog_resource = $this -> input -> post('catalog_resource');
		$catalog_perm = $this -> input -> post('catalog_perm');
		
		$this->handle_permission($role_id, $user_resource, $user_perm, self::PERM_INSERT);
		$this->handle_permission($role_id, $role_resource, $role_perm, self::PERM_INSERT);
		$this->handle_permission($role_id, $article_resource, $article_perm, self::PERM_INSERT);
		$this->handle_permission($role_id, $catalog_resource, $catalog_perm, self::PERM_INSERT);
		
		$this -> show($id);
		}
	}

	function update() {
		$this -> setFormValidation();

		if ($this -> input -> post('action') == "save") {
			$role_id = $this -> input -> post('id');
			if ($this -> form_validation -> run() == true) {
				$this -> model -> update($role_id, $this -> init_input_data());
				
				//update permission
				$user_resource = $this -> input -> post('user_resource');
				$user_perm = $this -> input -> post('user_perm');
				
				$role_resource = $this -> input -> post('role_resource');
				$role_perm = $this -> input -> post('role_perm');
				
				$article_resource = $this -> input -> post('article_resource');
				$article_perm = $this -> input -> post('article_perm');
				
				$catalog_resource = $this -> input -> post('catalog_resource');
				$catalog_perm = $this -> input -> post('catalog_perm');
				
				$this->handle_permission($role_id, $user_resource, $user_perm, self::PERM_UPDATE);
				$this->handle_permission($role_id, $role_resource, $role_perm, self::PERM_UPDATE);
				$this->handle_permission($role_id, $article_resource, $article_perm, self::PERM_UPDATE);
				$this->handle_permission($role_id, $catalog_resource, $catalog_perm, self::PERM_UPDATE);
				
				$this -> show($role_id);
			} else {//validation fail
				//TODO: need to fix
				$data = $this -> model -> get($id);
				$this -> load -> view($this -> _edit_path, $data);
			}
		} else {
			$id = $this -> uri -> segment(self::URI_SEGMENT);
			$data['role'] = (array)$this -> model -> get($id);
			$data['perms'] = $this -> permission -> get_many_by(array('role' => $id));
			$this->load->view($this -> _edit_path, $data);
		}
	}

	private function handle_permission($role_id, $resource, $perm, $action) {
		$perm_arr = array(
			'role' => $role_id,
			'resource' => $resource,
		);
		if(is_array($perm)) { //user choose a permission
			array_search('Read', $perm) > -1 ?
				add_to_array($perm_arr, array('read' => 1)) : add_to_array($perm_arr, array('read' => 0));
				
			array_search('Write', $perm) > -1 ?
				add_to_array($perm_arr, array('write' => 1)) : add_to_array($perm_arr, array('write' => 0));
				
			array_search('Modify', $perm) > -1 ?
				add_to_array($perm_arr, array('modify' => 1)) : add_to_array($perm_arr, array('modify' => 0));
				
			array_search('Delete', $perm) > -1 ?
				add_to_array($perm_arr, array('delete' => 1)) : add_to_array($perm_arr, array('delete' => 0));
			if($action == self::PERM_INSERT) {
				$this -> permission -> insert($perm_arr);
			} else {
				$perm_id = $this -> input -> post('perm_id');
				$this -> permission -> update($perm_id, $perm_arr);
			}
		}
	}
	
	public static function register_resource() {
		$resource = array(
			'id' => 4,
			'name' => 'Role',
			'description' =>''
		);
	}
}
?>